package com.pw.study.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

public class MyHDFSCDemo {
    FileSystem fs = null;

    @Before
    public void setUp() throws URISyntaxException, IOException, InterruptedException {
        //连接hdfs系统
        fs = FileSystem.get(new URI("hdfs://hadoop41:8020"), new Configuration(), "myhadoop");
    }

    @After
    public void setEnd() {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除
     */
    @Test
    public void delDir() throws IOException {
          fs.delete(new Path("/china"),true);
    }

    /**
     * 遍历目录与文件
     */
    @Test
    public void listDir() throws IOException {
        FileStatus[] status = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : status) {
            if (fileStatus.isFile()) {
                System.out.println("这是文件。。。。。");
                String name = fileStatus.getPath().getName();
                System.out.println("文件名：" + name);

                System.out.println("文件所存在的位置：");
                BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, 0, 10);
                System.out.println("文件块位置：" + Arrays.toString(blockLocations));
            }
            if (fileStatus.isDirectory()) {
                System.out.println("这是目录。。。。。。" + fileStatus.getPath());
                showDir(fileStatus.getPath());
            }
        }
    }

    private void showDir(Path path) throws IOException {
        FileStatus[] status = fs.listStatus(path);
        for (FileStatus fileStatus : status) {
            if (fileStatus.isFile()) {
                System.out.println("这是文件。。。。。");
                String name = fileStatus.getPath().getName();
                System.out.println("文件名：" + name);

                System.out.println("文件所存在的位置：");
                BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, 0, 10);
                System.out.println("文件块位置：" + Arrays.toString(blockLocations));
            }
            if (fileStatus.isDirectory()) {
                System.out.println("这是目录。。。。。。" + fileStatus.getPath());
                showDir(fileStatus.getPath());
            }
        }
    }


    /**
     * 创建目录
     */
    @Test
    public void testMkdir() {
        try {
            fs.mkdirs(new Path("/good"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 遍历文件
     */
    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus fileStatus = listFiles.next();
            //输出文件信息

            System.out.println("权限：" + fileStatus.getPermission());
            System.out.println("所属主：" + fileStatus.getOwner());
            System.out.println("所属组：" + fileStatus.getGroup());
            System.out.println("文件大小：" + fileStatus.getLen());
            System.out.println("时间：" + fileStatus.getAccessTime());
            System.out.println("备份数：" + fileStatus.getReplication());
            System.out.println("文件名：" + fileStatus.getPath().getName());
            System.out.println("---------------------------");
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            System.out.println("块所在的位置" + Arrays.toString(blockLocations));

        }
    }
}
